//package com.zhuiyun.project.api.dukacard.controller;
//
//import com.alibaba.fastjson2.JSONObject;
//import com.zhuiyun.project.api.coachmanagement.entity.CoachManagement;
//import com.zhuiyun.project.api.dukacard.config.WebSocketServer;
//import com.zhuiyun.project.api.dukacard.model.ReservationFiveTableModel;
//import com.zhuiyun.project.api.memberTypeManagement.entity.MemberCardNumber;
//import com.zhuiyun.project.api.memberUserManagement.mapper.MemberUserManagementMapper;
//import com.zhuiyun.project.api.memberUserManagement.model.MemberUserAndTypeModel;
//import com.zhuiyun.project.api.memberUserManagement.service.MemberUserManagementService;
//import com.zhuiyun.project.api.coachmanagement.model.CoachUpPasswordModel;
//import com.zhuiyun.project.api.reservation.entity.Reservation;
//import com.zhuiyun.project.api.reservation.mapper.ReservationMapper;
//import com.zhuiyun.project.api.vipPlan.model.ActionListModel;
//import com.zhuiyun.project.utils.ErrorCardUtils;
//import com.zhuiyun.project.utils.RestUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.lang.reflect.Field;
//import java.util.List;
//import java.util.concurrent.CompletableFuture;
//
///**
// * @ClassName MemberTypeController
// * @Description TODO 定时读卡
// * @Author WX
// * @Date 2023/11/2 11:36
// **/
//@Component
//public class Main {
//    @Autowired
//    private MemberUserManagementMapper memberUserManagementMapper;
//    @Autowired
//    private ReservationMapper reservationMapper;
//
//    /**
//    * @Author wx
//    * @Description 每秒执行读卡任务
//    * @Date 2023/12/7 11:39
//    * @Return
//     */
//    @Scheduled(cron = "*/1 * * * * ?") // 每1秒执行一次
//    public void readCardAsync() {
//        // 异步处理任务，不等待结果
//        CompletableFuture.runAsync(() -> {
//            String s = RestUtils.get("/cardReaders/readCard");
//            if (s != null) {
//                try {
//                    JSONObject responseJson = JSONObject.parseObject(s); // 将返回的字符串解析为JSONObject
//                    String venueUniqueIdentifier = responseJson.getString("customValue"); // 提取 customValue 并命名为 venueUniqueIdentifier
//                    String membershipCardNumber = responseJson.getString("readResult"); // 提取 readResult 并命名为 membershipCardNumber
//                    // 通过租户标识和卡号查询用户信息
//                    MemberUserAndTypeModel byMembershipCardNumber = memberUserManagementMapper.getByMembershipCardNumber(venueUniqueIdentifier, membershipCardNumber);
//
//                    if (byMembershipCardNumber != null){
//
//                        // 判断教练邀请码是否拥有
//                        if (byMembershipCardNumber.getInvitationCode() != null &&! byMembershipCardNumber.getInvitationCode().equals("null")){
//                            String invitationCode =byMembershipCardNumber.getInvitationCode();
//                            // 查询教练信息
//                           CoachManagement coachManagement =memberUserManagementMapper.getByInvitationCode(invitationCode,venueUniqueIdentifier);
//                           byMembershipCardNumber.setCoachName(coachManagement.getCoachName()) ;
//                        }
//
//                    }
//                    // 查询约课信息
//                    List<ActionListModel> reservationList = reservationMapper.selectListById(venueUniqueIdentifier,membershipCardNumber);
//                    if (byMembershipCardNumber != null) {
//                        MemberCardNumber memberCardNumber = memberUserManagementMapper.selectCardNumberByToday(venueUniqueIdentifier, membershipCardNumber);
//                        Integer cardNumber ;
//                        if (memberCardNumber != null){
//                            cardNumber = memberCardNumber.getCardNumber();
//                        }else {
//                            cardNumber = 0;
//                        }
//                        // 将查询结果转换为JSON格式
//                        JSONObject memberUserJson = new JSONObject();
//
//                        // 使用反射获取对象的所有字段并放入JSON中
//                        Field[] fields = byMembershipCardNumber.getClass().getDeclaredFields();
//                        for (Field field : fields) {
//                            field.setAccessible(true);
//                            memberUserJson.put(field.getName(), field.get(byMembershipCardNumber));
//                        }
//
//                        // 添加reservationList到JSON中
//                        memberUserJson.put("reservationList", reservationList);
//                        memberUserJson.put("cardNumber", cardNumber);
//                        // 将JSON格式的查询结果发送到客户端
//                        WebSocketServer.sendMessage(memberUserJson.toString());
//                    }else {
//                        // 如果查询结果为null，则返回venueUniqueIdentifier和membershipCardNumber的值
//                        JSONObject responseJson2 = new JSONObject();
//                        responseJson2.put("venueUniqueIdentifier", venueUniqueIdentifier);
//                        responseJson2.put("membershipCardNumber", membershipCardNumber);
//                        WebSocketServer.sendMessage(responseJson2.toString());
//                    }
//
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//    }
//}
